package com.salton123.soulove

import android.content.Context
import com.salton123.ads.provider.AdMobProvider
import com.salton123.app.BaseApplication
import com.salton123.app.initial.future.BaseAppDelegate
import com.salton123.module.api.ModuleIncubator
import com.salton123.qa.QualityAssistant
import com.salton123.qa.QualityContext
import com.salton123.revenue.RevenueFacade
import com.salton123.soulove.common.widget.TRefreshHeader
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.footer.ClassicsFooter

/**
 * User: newSalton@outlook.com
 * Date: 2019/5/23 15:40
 * ModifyTime: 15:40
 * Description:
 */
class XApp : BaseApplication() {
    companion object {
        init {
            //设置全局默认配置（优先级最低，会被其他设置覆盖）
            SmartRefreshLayout.setDefaultRefreshInitializer { _: Context?, layout: RefreshLayout? ->
                //开始设置全局的基本参数（可以被下面的DefaultRefreshHeaderCreator覆盖）
                layout?.setHeaderMaxDragRate(1.5f)
            }
            //设置全局的Header构建器
            SmartRefreshLayout.setDefaultRefreshHeaderCreator { context: Context?, _: RefreshLayout? ->
                TRefreshHeader(context)
            }
            //设置全局的Footer构建器
            SmartRefreshLayout.setDefaultRefreshFooterCreator { context: Context?, _: RefreshLayout? ->
                val classicsFooter = ClassicsFooter(context)
                classicsFooter.setTextSizeTitle(12f)
                classicsFooter.setDrawableSize(16f)
                classicsFooter.setFinishDuration(0)
                classicsFooter
            }
        }
    }

    val moduleIncubator = ModuleIncubator()
    override fun getAppDelegate(): BaseAppDelegate {
        return object : BaseAppDelegate() {
            override fun lowPriority() {
                super.lowPriority()
                moduleIncubator.lowPriority(sInstance)
            }

            override fun mediumPriority() {
                super.mediumPriority()
                moduleIncubator.mediumPriority(sInstance)
            }

            override fun highPriority() {
                super.highPriority()
                moduleIncubator.highPriority(sInstance)
            }

            override fun runOnMainProcessMainThread() {
                super.runOnMainProcessMainThread()

                moduleIncubator.onCreate(sInstance)
            }
        }
    }

    override fun onCreate() {
        super.onCreate()
        QualityAssistant.install(QualityContext(sInstance))
        RevenueFacade.init(
            this,
            AdMobProvider(this),
//            IronProvider(this),
//            AudienceProvider(this),
//            UnityProvider(this)
        )
    }

    override fun attachBaseContext(base: Context?) {
//        ClassPath.IS_DEBUG = BuildConfig.APP_DEVELOP
        super.attachBaseContext(base)
        moduleIncubator.initModules()
        moduleIncubator.attachBaseContext(sInstance)
        CommonToolKits.arouter().bugly().fragmentation().permission()
    }
}